﻿using AdminApi.Extension.Permission;
using AdminApi.Extension.Permission.Attributes;
using AdminApi.Extension.Permission.Model;
using Application.Admin;
using Domain.Types;
using DTO.Admin;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using Infrastructure.Error;
using System.Linq;

namespace AdminApi.Controllers
{
    [ApiController]
    public class TaskController : ControllerBase
    {
        public TaskAdminApplication taskAdminApplication { get; set; }
        public ILogger<TaskCategoryController> _logger { get; set; }

        [Route("api/Task/GetTaskList")]
        [HttpGet, PerssionMenu("任务列表", "系统任务管理", Index = "/task_list")]
        public ActionResult<IList<TaskCategoryOutput>> GetTaskList(string name, TaskStatus? taskStatus, TaskAuditStatus? taskAuditStatus, int page, int size)
        {
            try
            {
                var reps = taskAdminApplication.GetTaskList(name, taskStatus, taskAuditStatus, page, size,out int totalcount);

               


                return this.AdminSuccess(reps, "成功", totalcount);
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙!");
            }
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="taskID"></param>
        /// <param name="taskAuditID"></param>
        /// <param name="taskAuditStatus"></param>
        /// <param name="remark">备注</param>
        /// <returns></returns>
        [Route("api/Task/AuditTask")]
        [HttpPost, PerssionMenu("审核", "系统任务管理-任务列表", MenuType = MenuType.修改)]
        public ActionResult<bool> AuditTask(AuditTaskInput auditTaskInput)
        {
            try
            {
                Dictionary<string, object> payLoad = (Dictionary<string, object>)this.HttpContext.Items["payLoad"];
                var adminID = (int)(long)payLoad["adminId"];
                var reps = taskAdminApplication.AuditTask(adminID, auditTaskInput.taskID, auditTaskInput.taskAuditID, auditTaskInput.taskAuditStatus, auditTaskInput.remark);
                return this.Success(reps, "成功");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙!");
            }
        }

    }
}
